﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using AttributeRouting;
using AttributeRouting.Web.Mvc;
using PH.Model.Schools;
using PH.Logic.Schools;
namespace PH.Web.Management.Areas.School.Controllers
{
    [RouteArea("School")]
    [RoutePrefix("Student")]
    [Authorize]
    public class StudentController : Controller
    {
        [Route("")]
        [Filters.CurrentSchool]
        public ActionResult Index()
        {
            return View();
        }

        [Route("Edit/{schGuid:guid}/{guid:guid?}")]
        public ActionResult Edit(Guid schGuid,Guid? guid)
        {
            StudentModel model = null;
            var school = SchoolLogic.GetSchool(schGuid);
            if (school != null)
            {
                if (guid != null)
                    model = StudentLogic.GetStudent(guid.Value);
                else
                    model = new StudentModel() { SchoolID = school.ID };
                ViewBag.Classes = ClassLogic.GetClasses(new ClassQuery() { SchoolGuid = schGuid });
                return PartialView(model);
            }
            else
                throw new HttpException("无效的学校编号");
        }

        [Route("Export")]
        [Filters.CurrentSchool]
        public ActionResult Export()
        {
            try
            {
                StudentQuery sea = new StudentQuery();
                if (!string.IsNullOrEmpty(Request.Form.Get("classGuid")))
                    sea.ClassGuid = new Guid(Request.Form.Get("classGuid"));
                sea.Key = Request.Form.Get("key");
                sea.SchoolGuid = PH.Web.Global.WebContext.CurrentSchool.Guid;
                if (!string.IsNullOrEmpty(Request.Form.Get("gradeCode")))
                    sea.GradeCode = int.Parse(Request.Form.Get("gradeCode"));
                var lst = StudentLogic.GetStudents(sea);
                if (lst.Count > 0)
                {
                    Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();
                    workbook.Open(Server.MapPath("/RootFiles/Templates/template_student.xls"));
                    var cells = workbook.Worksheets[0].Cells;
                    for (var i = 0; i < lst.Count; i++)
                    {
                        var item = lst[i];
                        cells[i + 1, 8].PutValue(item.StudentNo);
                        cells[i + 1, 4].PutValue(item.RealName);
                        cells[i + 1, 5].PutValue(item.Sex);
                        cells[i + 1, 1].PutValue(item.ClassName);
                        cells[i + 1, 10].PutValue(item.Mobile);
                        cells[i + 1, 11].PutValue(item.OriSchoolCode);
                    }
                    var buffer = workbook.SaveToStream().ToArray();
                    return File(buffer, "application/octet-stream", "学生数据.xls");
                }
                else
                    return Content("暂无数据");
            }
            catch (Exception ex)
            {
                Common.Logger.Error("", ex);
                return Content("服务器运行出错，请联系开发商。");
            }
        }

        [Route("IDNO")]
        [Filters.CurrentSchool]
        public ActionResult IDNO()
        {
            return View();
        }

        [Route("ExportIDNO")]
        [Filters.CurrentSchool]
        public ActionResult ExportIDNO()
        {
            try
            {
                StudentQuery sea = new StudentQuery();
                sea.SchoolGuid = PH.Web.Global.WebContext.CurrentSchool.Guid;
                var lst = StudentLogic.GetStudents(sea);
                if (lst.Count > 0)
                {
                    Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();
                    workbook.Open(Server.MapPath("/RootFiles/Templates/idno.xls"));
                    var cells = workbook.Worksheets[0].Cells;
                    for (var i = 0; i < lst.Count; i++)
                    {
                        var item = lst[i];
                        cells[i + 1, 0].PutValue(item.ID);
                        cells[i + 1, 1].PutValue(item.ClassName);
                        cells[i + 1, 2].PutValue(item.RealName);
                        cells[i + 1, 3].PutValue(item.StudentNo);
                    }
                    var buffer = workbook.SaveToStream().ToArray();
                    return File(buffer, "application/octet-stream", "学生数据.xls");
                }
                else
                    return Content("暂无数据");
            }
            catch (Exception ex)
            {
                Common.Logger.Error("", ex);
                return Content("服务器运行出错，请联系开发商。");
            }
        }
    }
}
